package wolfUndSchafe;

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Display
{

  public JFrame frame = new JFrame();

  protected JButton b0 = new JButton("");
  protected JButton b1 = new JButton("");
  protected JButton b2 = new JButton("");
  protected JButton b3 = new JButton("");
  protected JButton b4 = new JButton("");
  protected JButton b5 = new JButton("");
  protected JButton b6 = new JButton("");
  protected JButton b7 = new JButton("");
  protected JButton b8 = new JButton("");
  protected JButton b9 = new JButton("");
  protected JButton b10 = new JButton("");
  protected JButton reset = new JButton();
  protected JButton[] buttons = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10};
  
  protected JList chooseStrat;
  
  private BackgroundGraphics grafik = new BackgroundGraphics();
  
  private Game game;

  protected JLabel txtWonOrLost = new JLabel("");
  protected JLabel strategieLabel = new JLabel("Strategy:");

  protected int gameCount = 0;
  protected JLabel txtGame = new JLabel("Game:   " + gameCount);

  protected int loseCount = 0;
  protected JLabel txtLosses = new JLabel("Losses:   " + loseCount);

  protected int winCount = 0;
  protected JLabel txtWins = new JLabel("Wins:   " + winCount);

  public Display()
  {
    initializeDisplay();
  }

  public void initializeDisplay()
  {
    frame.setTitle("Hounds and Hare");

    String[] strats = {"Random", "Minimal", "Optimized", "Learning"};
    chooseStrat = new JList(strats);

    int frameWidth = 400;
    int frameHeight = 400;

    b0.setBackground(Color.BLACK);
    b1.setBackground(Color.BLACK);
    b3.setBackground(Color.BLACK);
    b5.setBackground(Color.RED);

    int bSize = 21;
    int buttonDistance = 8;

    b0.setBounds(frameWidth / buttonDistance * 4 - bSize / 2, frameHeight
        / buttonDistance * 5, bSize, bSize);
    b1.setBounds(frameWidth / buttonDistance * 3 - bSize / 2, frameHeight
        / buttonDistance * 4, bSize, bSize);
    b2.setBounds(frameWidth / buttonDistance * 4 - bSize / 2, frameHeight
        / buttonDistance * 4, bSize, bSize);
    b3.setBounds(frameWidth / buttonDistance * 5 - bSize / 2, frameHeight
        / buttonDistance * 4, bSize, bSize);
    b4.setBounds(frameWidth / buttonDistance * 3 - bSize / 2, frameHeight
        / buttonDistance * 3, bSize, bSize);
    b5.setBounds(frameWidth / buttonDistance * 4 - bSize / 2, frameHeight
        / buttonDistance * 3, bSize, bSize);
    b6.setBounds(frameWidth / buttonDistance * 5 - bSize / 2, frameHeight
        / buttonDistance * 3, bSize, bSize);
    b7.setBounds(frameWidth / buttonDistance * 3 - bSize / 2, frameHeight
        / buttonDistance * 2, bSize, bSize);
    b8.setBounds(frameWidth / buttonDistance * 4 - bSize / 2, frameHeight
        / buttonDistance * 2, bSize, bSize);
    b9.setBounds(frameWidth / buttonDistance * 5 - bSize / 2, frameHeight
        / buttonDistance * 2, bSize, bSize);
    b10.setBounds(frameWidth / buttonDistance * 4 - bSize / 2, frameHeight
        / buttonDistance, bSize, bSize);
    reset.setBounds(frameWidth / buttonDistance * 4 - 103 / 2,
        frameHeight - 80, 103, 20);
    txtWonOrLost.setBounds(frameWidth / 2 - 25, frameHeight / buttonDistance
        * 6 - 17, 100, bSize);
    txtGame.setBounds(30, 20, 100, bSize);
    txtLosses.setBounds(30, 45, 100, bSize);
    txtWins.setBounds(30, 70, 100, bSize);
    
    chooseStrat.setBounds(300, 50, 60, 72);
    chooseStrat.setSelectedIndex(2);
    strategieLabel.setBounds(300, 20, 60, 30);
    
    chooseStrat.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e)
      {
        if(chooseStrat.getSelectedIndex()==0)
          game.wolf.setStrategie(new RandomStrategie());
        else if(chooseStrat.getSelectedIndex()==1)
          game.wolf.setStrategie(new MinimalStrategie());
        else if(chooseStrat.getSelectedIndex()==2)
          game.wolf.setStrategie(new OptimizedStrategy());
        else if(chooseStrat.getSelectedIndex()==3)
          game.wolf.setStrategie(new LearningStrategy());
      }
    });

    frame.getContentPane().add(b0);
    frame.getContentPane().add(b1);
    frame.getContentPane().add(b2);
    frame.getContentPane().add(b3);
    frame.getContentPane().add(b4);
    frame.getContentPane().add(b5);
    frame.getContentPane().add(b6);
    frame.getContentPane().add(b7);
    frame.getContentPane().add(b8);
    frame.getContentPane().add(b9);
    frame.getContentPane().add(b10);
    frame.getContentPane().add(reset);
    frame.getContentPane().add(txtWonOrLost);
    frame.getContentPane().add(txtGame);
    frame.getContentPane().add(txtLosses);
    frame.getContentPane().add(txtWins);
    frame.getContentPane().add(strategieLabel);
    
    frame.getContentPane().add(chooseStrat);
    
    frame.getContentPane().add(grafik);

    frame.pack();

    frame.setBounds(300, 100, frameWidth, frameHeight);
    frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);
  }
  
  public void showWinner(String winner)
  {
    if(winner.equals("PLAYER"))
    {
      reset.setText("Play again");
      txtWonOrLost.setText(" You won!");
      gameCount++;
      txtGame.setText("Game:   " + gameCount);
      winCount++;
      txtWins.setText("Wins:   " + winCount);
      
      enableAllButtons(false);
    }
    else if(winner.equals("COMP"))
    {
      reset.setText("Play again");
      txtWonOrLost.setText(" You lost!");
      gameCount++;
      txtGame.setText("Game:   " + gameCount);
      loseCount++;
      txtLosses.setText("Losses:   " + loseCount);
      enableAllButtons(false);
    }
  }

  public void updateDisplay(Game game)
  {
    this.game = game;
    
    for(int i = 0; i < game.field.length; i++)
    {
      if(game.field[i].contains("WOLF"))
      {
        buttons[i].setBackground(Color.RED);
        buttons[i].setEnabled(false);
      }
      else if(game.field[i].contains("SHEEP"))
      {
        buttons[i].setBackground(Color.BLACK);
        buttons[i].setEnabled(true);
      }
      else
      {
        buttons[i].setBackground(null);
      }
    }
    enableAllFreeSpots(false);
  }

  public void enableAllSheep(boolean enable)
  {
    for(int i = 0; i < game.field.length; i++)
    {
      if(game.field[i].contains("SHEEP"))
      {
        buttons[i].setEnabled(enable);
        buttons[i].setBackground(Color.BLACK);
      }
    }
  }

  public void enableAllValidSpots(int position, boolean enable)
  {
    for(int i = 0; i < game.field.length; i++)
    {
      if(MoveRules.isValidSheepMove(game.field, position, i))
        buttons[i].setEnabled(true);
      else
        buttons[i].setEnabled(false);
    }
  }

  public void enableAllFreeSpots(boolean enable)
  {
    for(int i = 0; i < game.field.length; i++)
    {
      if(MoveRules.isFreeSpot(i, game.field))
        buttons[i].setEnabled(enable);
    }
  }

  public void enableAllButtons(boolean enable)
  {
    for(int i = 0; i < game.field.length; i++)
    {
      buttons[i].setEnabled(enable);
    }
  }

}
